local chonghuo = fk.CreateSkill {
  name = "th_chonghuo",
  max_phase_use_time = 1
}

chonghuo:addEffect("active", {
  name = "th_chonghuo",
  anim_type = "support",
  prompt = "#th_chonghuo",
  max_phase_use_time = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and self:withinTimesLimit(player, Player.HistoryPhase)
  end,
  min_card_num = 1,
  -- max_card_num = 2,
  card_filter = function(self, player, to_select, selected)
    return Fk:currentRoom():getCardArea(to_select) ~= Card.PlayerEquip
  end,
  target_num = 1,
  target_filter = function(self, player, to_select, selected)
    return to_select ~= player and #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local cards = table.map(effect.cards, Util.Id2CardMapper)
    room:obtainCard(target, cards, false, fk.ReasonGive)
    for _, card in ipairs(cards) do
      room:setCardMark(card, "@@th_chonghuo_chong", player.id)
    end
  end,
}, { check_skill_limit = true })

chonghuo:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do ---@param move MoveCardsData
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card:getMark("@@th_chonghuo_chong") ~= 0 then
            return true
          end
        end
      end
    end
  end,
  trigger_times = function(self, event, target, player, data)
    local move_event = player.room.logic:getMostRecentEvent(GameEvent.MoveCards)
    local max = event:getSkillData(self, "th_chonghuo_" .. player.id .. "_" .. move_event.id .. "max")
    if max and max > 0 then
      return max
    end
    local cards = {}
    for _, move in ipairs(data) do ---@param move MoveCardsData
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card:getMark("@@th_chonghuo_chong") ~= 0 then
            table.insert(cards, {info.cardId, move.moveReason})
          end
        end
      end
    end
    event:setSkillData(self, "th_chonghuo_" .. player.id .. "_" .. move_event.id .. "max", #cards)
    event:setSkillData(self, "th_chonghuo_" .. player.id .. "_" .. move_event.id, cards)
    return #cards
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    event:setSkillData(self, "cancel_cost", false)

    local move_event = room.logic:getMostRecentEvent(GameEvent.MoveCards)
    local dat = event:getSkillData(self, "th_chonghuo_" .. player.id .. "_" .. move_event.id)
    local single = table.remove(dat, 1)
    if single and room:getCardArea(single[1]) == Card.DiscardPile then
      event:setSkillData(self, "th_chonghuo_" .. player.id, dat)

      event:setCostData(self, single)
      self:doCost(event, target, player, data)
    else
      local card = Fk:getCardById(single[1])
      room:setCardMark(card, "@@th_chonghuo_chong", 0)
    end

    event:setSkillData(self, "cancel_cost", false)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local card = Fk:getCardById(dat[1])
    local reason = dat[2]
    local owner = room:getPlayerById(card:getMark("@@th_chonghuo_chong"))
    room:setCardMark(card, "@@th_chonghuo_chong", 0)
    if owner:isAlive() then
      local cost = {owner, card}
      cost.can_return = not table.contains({fk.ReasonUse, fk.ReasonResponse}, reason)
      event:setCostData(self, cost)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    if dat.can_return then
      room:obtainCard(dat[1], dat[2], true, fk.ReasonJustMove)
    else
      dat[1]:drawCards(1, chonghuo.name)
    end
  end
})

Fk:loadTranslationTable{
  ["th_chonghuo"] = "虫惑",
  [":th_chonghuo"] = "出牌阶段限一次，你可以交给一名其他角色任意张手牌，称为“虫”；“虫”进入弃牌堆后，若因使用或打出进入弃牌堆，你摸一张牌，否则你获得之。",

  ["#th_chonghuo"] = "虫惑：交给一名其他角色任意张手牌，这些牌进入弃牌堆后，根据进入方式：你获得之或摸一张牌",

  ["@@th_chonghuo_chong"] = "虫",
}

return chonghuo